package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.ClassInfo;
import com.fsc.entity.domain.RewardPunish;
import com.fsc.entity.domain.StudentInfo;
import com.fsc.entity.dto.RewardPushDTO;
import com.fsc.entity.vo.ClassInfoVO;
import com.fsc.entity.vo.RewardPunishVO;
import com.fsc.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: RavenLyn
 * @Date: 2024-06-14
 */
@RestController
@RequestMapping("/reward_punish")
@Slf4j
public class RewardPunishController {

    @Autowired
    private RewardPunishService rewardPunishService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private StuPartTeachService stuPartTeachService;
    @Autowired
    private LoginInfoService loginInfoService;

    /**
     * 获取奖励数据
     * @return
     */
    @GetMapping("/reward")
    public ResultWrapper<List<RewardPunishVO>> getReward(){
        List<RewardPunish> list1;
        QueryWrapper<RewardPunish> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("type",1);
        list1 = rewardPunishService.list(wrapper1);
        int[] ids = null;
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        //0：领导，1：老师，2：家长，3：学生
        if(role == 0){  //领导
            ids = list1.stream()  // 将 list1 转换为 Stream
                    .mapToInt(RewardPunish::getStudentId)  // 提取每个元素的 id
                    .toArray();  // 转换为 int 数组

        }
        if(role == 1){  //老师
            Integer teacherId = loginId;
            int[] rawIds = stuPartTeachService.getStuIdsByTeaId(teacherId);
            ids = rewardPunishService.getIdsByRawIds(rawIds,1);
        }
        if(role == 2){  //家长
            Integer parentId = loginId;
            int[] rawIds = stuPartTeachService.getStuIdsByParId(parentId);
            ids = rewardPunishService.getIdsByRawIds(rawIds,1);
        }

        //用ids去stu_part_teach表获取class_id
        int[] classIds = new int[ids.length];
        for (int i = 0; i < ids.length; i++) {
            classIds[i] = stuPartTeachService.getClassIdByStuId(ids[i]);
        }

        List<Integer> idList = new ArrayList<>();//再转为List
        for (int i = 0; i < ids.length; i++) {
            idList.add(ids[i]);
        }
        QueryWrapper<StudentInfo> wrapper2 = new QueryWrapper<>();
        wrapper2.in("student_id",idList);
        List<StudentInfo> list2 = studentInfoService.list(wrapper2);

        QueryWrapper<RewardPunish> wrapper = new QueryWrapper<>();
        wrapper.in("student_id",idList).eq("type",1);
        list1 = rewardPunishService.list(wrapper);

        List<RewardPunishVO> voList = new ArrayList<>();

        for (int i = 0; i < list1.size(); i++) {
            RewardPunishVO newOne = new RewardPunishVO();
            newOne.setStudentId(idList.get(i));
            newOne.setClassId(classIds[i]);
            BeanUtils.copyProperties(list1.get(i),newOne);//拷贝list1中的time和description
            for (int j = 0; j < list2.size(); j++) {
                if(newOne.getStudentId() == list2.get(j).getStudentId()){
                    newOne.setStudentName(list2.get(j).getStudentName());
                }
            }
            voList.add(newOne);
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,voList);
    }

    /**
     * 获取惩罚数据
     * @return
     */
    @GetMapping("/punish")
    public ResultWrapper<List<RewardPunishVO>> getPunish(){
        List<RewardPunish> list1;
        QueryWrapper<RewardPunish> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("type",0);
        list1 = rewardPunishService.list(wrapper1);
        int[] ids = null;
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        //0：领导，1：老师，2：家长，3：学生
        if(role == 0){  //领导
            ids = list1.stream()  // 将 list1 转换为 Stream
                    .mapToInt(RewardPunish::getStudentId)  // 提取每个元素的 id
                    .toArray();  // 转换为 int 数组

        }
        if(role == 1){  //老师
            Integer teacherId = loginId;
            int[] rawIds = stuPartTeachService.getStuIdsByTeaId(teacherId);
            ids = rewardPunishService.getIdsByRawIds(rawIds,0);
        }
        if(role == 2){  //家长
            Integer parentId = loginId;
            int[] rawIds = stuPartTeachService.getStuIdsByParId(parentId);
            ids = rewardPunishService.getIdsByRawIds(rawIds,0);
        }
        //用ids去stu_part_teach表获取class_id
        int[] classIds = new int[ids.length];
        for (int i = 0; i < ids.length; i++) {
            classIds[i] = stuPartTeachService.getClassIdByStuId(ids[i]);
        }

        List<Integer> idList = new ArrayList<>();//再转为List
        for (int i = 0; i < ids.length; i++) {
            idList.add(ids[i]);
        }
        QueryWrapper<StudentInfo> wrapper2 = new QueryWrapper<>();
        wrapper2.in("student_id",idList);
        List<StudentInfo> list2 = studentInfoService.list(wrapper2);

        QueryWrapper<RewardPunish> wrapper = new QueryWrapper<>();
        wrapper.in("student_id",idList).eq("type",0);
        list1 = rewardPunishService.list(wrapper);

        List<RewardPunishVO> voList = new ArrayList<>();

        for (int i = 0; i < list1.size(); i++) {
            RewardPunishVO newOne = new RewardPunishVO();
            newOne.setStudentId(idList.get(i));
            newOne.setClassId(classIds[i]);
            BeanUtils.copyProperties(list1.get(i),newOne);//拷贝list1中的id,studentId,time和description
            for (int j = 0; j < list2.size(); j++) {
                if(newOne.getStudentId() == list2.get(j).getStudentId()){
                    newOne.setStudentName(list2.get(j).getStudentName());
                }
            }
            voList.add(newOne);
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,voList);
    }

    /**
     * 获取班级数据
     * @return
     */
    @GetMapping("/classes")
    public ResultWrapper<List<ClassInfoVO>> getClasses() {
//        List<ClassInfo> list = classInfoService.list();
        List<ClassInfo> list = new ArrayList<>();
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        if(role == 0){ //领导
            list = classInfoService.list();
        }
        if(role == 1){ //老师
            list = classInfoService.getClassByTeaId(loginId);
        }

        List<ClassInfoVO> classesList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++){
            ClassInfoVO newOne = new ClassInfoVO();
            BeanUtils.copyProperties(list.get(i),newOne);
            classesList.add(newOne);
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,classesList);
    }

    /**
     * 更新学生奖罚信息
     * @param rewardPushDTO
     */
    @PutMapping("/update")
    public void updateInfo(@RequestBody RewardPushDTO rewardPushDTO){
        Long id = rewardPushDTO.getId();
        Date time = rewardPushDTO.getTime();
        String description = rewardPushDTO.getDescription();
        UpdateWrapper<RewardPunish> wrapper = new UpdateWrapper<>();
        wrapper.set("time",time)
                .set("description",description)
                .eq("id",id);
        rewardPunishService.update(wrapper);
    }

    /**
     * 删除学生奖罚信息
     * @param id
     */
    @DeleteMapping("/delete")
    public void deleteInfo(Long id){
        QueryWrapper<RewardPunish> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        rewardPunishService.remove(wrapper);
    }

    @PostMapping("/add")
    public void addInfo(@RequestBody RewardPushDTO rewardPushDTO){
        int studentId = rewardPushDTO.getStudentId();
        Date time = rewardPushDTO.getTime();
        String description = rewardPushDTO.getDescription();
        int type = rewardPushDTO.getType();

        RewardPunish newOne = new RewardPunish();
        newOne.setStudentId(studentId);
        newOne.setTime(time);
        newOne.setDescription(description);
        newOne.setType(type);
        rewardPunishService.save(newOne);
    }

    @GetMapping("/all")
    public ResultWrapper<List<RewardPunishVO>> getAll(){
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);
        List<StudentInfo> stuInfoList = null;
        if(role == 0){ //领导
            stuInfoList = studentInfoService.list();
        }
        if(role == 1){ //老师
            List<Integer> idList = stuPartTeachService.getStudent(loginId);
            QueryWrapper<StudentInfo> wrapper = new QueryWrapper<>();
            wrapper.in("student_id",idList);
            stuInfoList = studentInfoService.list(wrapper);
        }

        //封装voList
        List<RewardPunishVO> voList = new ArrayList<>();
        for (int i = 0; i < stuInfoList.size(); i++) {
            RewardPunishVO newOne = new RewardPunishVO();
            BeanUtils.copyProperties(stuInfoList.get(i),newOne); //拷贝stuInfoList中的studentId和studentName
            voList.add(newOne);
        }
        return ResultWrapper.success(ReturnEnum.SUCCESS,voList);
    }
}
